package b.h.y.bj.base.menudetail;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.CharsetUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

import b.h.y.bj.R;
import b.h.y.bj.base.BaseMenuDetailPager;
import b.h.y.bj.domain.PhotoData;
import b.h.y.bj.global.GlobalContants;
import b.h.y.bj.util.CacheUtils;

/**
 * 菜单详情页-组图
 * Created by YHB on 2016/1/4.
 */
public class PhotoMenuDetailPager extends BaseMenuDetailPager {

    private ImageButton mBtnPhotoType;

    private boolean isListDisplay = true; //是否是列表展示

    private PhotoData photoData;

    private ArrayList<PhotoData.PhotoInfo> mPhotoList;
    private PhotoAdapter mAdapter;

    public PhotoMenuDetailPager(Activity activity, ImageButton mBtnPhotoType) {
        super(activity);
        this.mBtnPhotoType = mBtnPhotoType;
        this.mBtnPhotoType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeDisplayType();
            }
        });
    }

    @ViewInject(R.id.lv_photo)
    private ListView mLvPhoto;

    @ViewInject(R.id.gv_photo)
    private GridView mGvPhoto;

    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.menu_photo_pager, null);
        ViewUtils.inject(this, view);
        return view;
    }

    /**
     * 切换展示方式
     */
    private void changeDisplayType() {
        if (isListDisplay) { //是列表展示
            isListDisplay = false;
            mGvPhoto.setVisibility(View.VISIBLE);
            mLvPhoto.setVisibility(View.INVISIBLE);

            mBtnPhotoType.setImageResource(R.drawable.icon_pic_list_type);
        } else {
            isListDisplay = true;
            mGvPhoto.setVisibility(View.INVISIBLE);
            mLvPhoto.setVisibility(View.VISIBLE);

            mBtnPhotoType.setImageResource(R.drawable.icon_pic_grid_type);
        }
    }

    @Override
    public void initData() {
        String cache = CacheUtils.getCache(mActivity, GlobalContants.PHOTO_URL);
        if (!TextUtils.isEmpty(cache)) {
            parseData(cache);
        }
        fetchDataForServer();
    }

    /**
     * 获取数据
     */
    private void fetchDataForServer() {
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, GlobalContants.PHOTO_URL, new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> responseInfo) {
                String result = (String) responseInfo.result;
                CacheUtils.setCache(mActivity, GlobalContants.PHOTO_URL, result);
                parseData(result);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        });
    }

    /**
     * 格式化数据
     *
     * @param result
     */
    private void parseData(String result) {
        Gson gson = new Gson();
        photoData = gson.fromJson(result, PhotoData.class);
        mPhotoList = photoData.data.news;

        if (mPhotoList != null) {
            mAdapter = new PhotoAdapter();
            mLvPhoto.setAdapter(mAdapter);
            mGvPhoto.setAdapter(mAdapter);
        }
    }

    class PhotoAdapter extends BaseAdapter {

        private BitmapUtils utils;

        public PhotoAdapter() {
            utils = new BitmapUtils(mActivity);
            utils.configDefaultLoadingImage(R.drawable.news_pic_default);
        }

        @Override
        public int getCount() {
            return mPhotoList.size();
        }

        @Override
        public PhotoData.PhotoInfo getItem(int i) {
            return mPhotoList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            ViewHolder holder;
            if (view == null) {
                view = View.inflate(mActivity, R.layout.list_photo_item, null);

                holder = new ViewHolder();
                holder.tvTitle = (TextView) view.findViewById(R.id.tv_title);
                holder.ivPic = (ImageView) view.findViewById(R.id.iv_pic);

                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            PhotoData.PhotoInfo photoInfo = getItem(i);
            holder.tvTitle.setText(photoInfo.title);
            utils.display(holder.ivPic, photoInfo.listimage);
            return view;
        }
    }

    class ViewHolder {
        public TextView tvTitle;
        public ImageView ivPic;
    }
}
